from odoo import api, models, fields, _
from odoo.exceptions import UserError


class Language(models.Model):
    _name = 'info.language'
    _description = 'some languages'

    name = fields.Char(
        '编程语言名称',
        required=True
    )


class Company(models.Model):
    _inherit = "res.company"

    stamp = fields.Binary('公章')


class Users(models.Model):
    _inherit = "res.users"

    checked_persons = fields.Many2many(
        'stu.info',
        string='评价过的面试者'
    )


class User_comments(models.Model):
    _name = 'user.comments'
    _description = '面试官的评语'
    guest_id = fields.Many2one(
        'stu.info',
        string=u"面试人",
        default=lambda
            self: self.env['user.comments']._company_default_get('stu.info')

    )
    operator_uid = fields.Many2one(
        string=u"面试官的姓名",
        readonly=True,
        comodel_name='res.users',
        default=lambda
            self: self.env.uid)
    opinion = fields.Html(
        '面试官的建议',
    )


class Stu(models.Model):
    _name = 'stu.info'
    _description = '面试者信息'

    name = fields.Char(
        '姓名',
        required=True,
        readonly=True,
    )
    telephone = fields.Char(
        '联系电话',
        required=True,
        readonly=True,
    )
    q_q = fields.Char(
        'QQ号',
        required=True,
        readonly=True,
    )
    e_mail = fields.Char(
        'E_mail',
        required=True,
        readonly=True,
    )
    grade = fields.Selection([
        ('2017', '2017'),
        ('2018', '2018'),
        ('2019', '2019'),
    ], string='所在年级', defaut='2019', index=True, required=False, readonly=True, )

    school = fields.Selection([
        ('计算机与信息工程学院', '计算机与信息工程学院'),
        ('外国语学院', '外国语学院'),
        ('马克思主义学院', '马克思主义学院'),
        ('数学与统计学学院', '数学与统计学学院'),
        ('体育学院', '体育学院'),
        ('文学院', '文学院'),
        ('艺术学院', '艺术学院'),
        ('新闻传播学院', '新闻传播学院'),
        ('政法与公共管理学院', '政法与公共管理学院'),
        ('资源环境学院', '资源环境学院'),
        ('历史文化学院', '历史文化学院'),
        ('化学化工学院', '化学化工学院'),
        ('物理与电子科学学院', '物理与电子科学学院'),
        ('教育学院', '教育学院'),
        ('哲学院', '哲学院'),
        ('生命科学学院', '生命科学学院'),
        ('材料科学与工程学院', '材料科学与工程学院'),
        ('商学院', '商学院')
    ], string='所在学院', defaut='计算机与信息工程学院', index=True, required=False, readonly=True, )
    classroom = fields.Char(
        '所在班级',
        required=False,
        readonly=True,
    )
    adjust = fields.Selection([
        ('接受', '接受'),
        ('拒绝', '拒绝')
    ], string='调剂态度', defaut='接受', required=False, readonly=True, )

    sex = fields.Selection([
        ('男', '男'),
        ('女', '女')
    ], string='性别', defaut='男', required=False, readonly=True, )

    ability = fields.Selection([
        ('预处理组', '预处理组'),
        ('前端组', '前端组'),
        ('后端组', '后端组'),
        ('测试组', '测试组'),
        ('产品经理', '产品经理'),
    ], string='意向组别', defaut='预处理组', index=True, readonly=True, )
    # language = fields.Selection([
    #     ('python', 'python'),
    #     ('java', 'java'),
    #     ('golang', 'golang'),
    #     ('前端', '前端'),
    #     ('c#', 'c#'),
    #     ('c/c++ ', 'c/c++'),
    # ], string='曾了解过的编程语言', defaut='python', index=True, required=True)
    language = fields.Many2many(
        'info.language', string='曾了解过的编程语言', required=False, readonly=True,
    )
    self_introduction = fields.Html(
        '自我介绍',
        required=False,
        readonly=True,
    )

    self_interest = fields.Html(
        '兴趣方向',
        required=False,
        readonly=True,
    )
    self_plan = fields.Html(
        '入坊规划',
        required=False,
        readonly=True,
    )

    yes_number = fields.Integer(
        '同意通过人数',
        readonly=True,
        defaut=0
    )

    yes_people = fields.Text(
        '同意通过',
        defaut='',
        readonly=True,
    )
    operator_uid = fields.Many2one(
        string=u"操作员",
        readonly=True,
        comodel_name='res.users',
        compute='_change_operator'
    )
    current_checked = fields.Many2many(
        'res.users',
        string='已评评委',
    )
    is_checked = fields.Boolean(
        '当前用户是否已评审',
        default=False,
        compute='_count_checked',
        readonly=True,
    )
    is_one_success = fields.Boolean(
        '是否具有一票通过权限',
        default=True,
        compute='_one_success',
        readonly=True,
    )
    user_email = fields.Char(
        '用户邮箱'
    )

    house_ids = fields.One2many(
        'user.comments',
        'guest_id',
        string='面试官评语',
        required=True,
        ondelete='set null',
    )
    whether_through = fields.Boolean(
        '是否已经是通过人员',
        default=False,
        readonly=True,
    )

    @api.multi
    @api.depends('current_checked')
    def _count_checked(self):
        # for id in self.current_checked.ids:
        #     if id == self.operator_uid.id:
        #         self.is_checked = True
        #         break
        #     else:
        #         self.is_checked = False

        for record in self.current_checked:
            if record.id == self.operator_uid.id:
                self.is_checked = True
                break
            else:
                self.is_checked = False

    @api.multi
    @api.depends('operator_uid')
    def _one_success(self):
        if self.env.user.login != "3095329264@qq.com":
            self.is_one_success = True

    def _change_operator(self):
        self.operator_uid = self.env.uid

    def one_to_success(self):
        self.yes_number = self.yes_number + 4
        self.ensure_one()
        sql_query = """SELECT name FROM stu_success where telephone = '{telephone}'""".format(telephone=self.telephone)
        self.env.cr.execute(sql_query)
        result = self.env.cr.dictfetchall()
        if result:
            raise UserError(_('%s已经通过!') % self.name)
        else:
            self.whether_through = True
            # logger.info("Here is the name: %s", result)
            check_as_superuser = self.env['stu.success'].sudo()
            # sudo():使用现有数据集创建一个新运行环境，得到一个基于新运行环境的数据集的拷贝
            check_as_superuser.create({
                'id': self.id,
                'telephone': self.telephone,
                'name': self.name,
                'q_q': self.q_q,
                'grade': self.grade,
                'school': self.school,
                'classroom': self.classroom,
                'adjust': self.adjust,
                'sex': self.sex,
                'ability': self.ability,
                'self_introduction': self.self_introduction,
                'self_interest': self.self_interest,
                'self_plan': self.self_plan,
                'language': [(6, 0, self.language.ids)],
                'e_mail': self.e_mail
            })
            user_comments = self.env['user.comments'].search([('guest_id', '=', self.id)]).sudo()
            print(user_comments)
            check_as_superuser = self.env['stu.success'].search([('telephone', '=', self.telephone)]).sudo()
            for man in user_comments:
                self.env['user.success'].sudo().create({
                    'guest_id': check_as_superuser.id,
                    'operator_uid': man.operator_uid.id,
                    'opinion': man.opinion,
                })
            # sql_query2 = """DELETE FROM stu_info where id = '{id}'""".format(id=self.id)
            # self.env.cr.execute(sql_query2)
            return {
                'type': 'ir.actions.act_window',
                'view_type': 'kanban',
                'view_mode': 'kanban,tree,form',
                'res_model': 'stu.success',
                'target': 'new',
            }

    def to_success(self):
        self.yes_number = self.yes_number + 1
        self.sudo().write({'current_checked': [(4, self.env.user.id)]})
        if self.yes_number == 4:
            self.whether_through = True
            # logger.info("Here is the name: %s", result)
            check_as_superuser = self.env['stu.success'].sudo()
            # sudo():使用现有数据集创建一个新运行环境，得到一个基于新运行环境的数据集的拷贝
            check_as_superuser.create({
                'id': self.id,
                'telephone': self.telephone,
                'name': self.name,
                'q_q': self.q_q,
                'grade': self.grade,
                'school': self.school,
                'classroom': self.classroom,
                'adjust': self.adjust,
                'sex': self.sex,
                'ability': self.ability,
                'self_introduction': self.self_introduction,
                'self_interest': self.self_interest,
                'self_plan': self.self_plan,
                'language': [(6, 0, self.language.ids)],
                'e_mail': self.e_mail
            })
            user_comments = self.env['user.comments'].search([('guest_id', '=', self.id)]).sudo()
            print(user_comments)
            check_as_superuser = self.env['stu.success'].search([('telephone', '=', self.telephone)]).sudo()
            for man in user_comments:
                self.env['user.success'].sudo().create({
                    'guest_id': check_as_superuser.id,
                    'operator_uid': man.operator_uid.id,
                    'opinion': man.opinion,
                })
            # sql_query2 = """DELETE FROM stu_info where id = '{id}'""".format(id=self.id)
            # self.env.cr.execute(sql_query2)
            return {
                'type': 'ir.actions.act_window',
                'view_type': 'kanban',
                'view_mode': 'kanban,tree,form',
                'res_model': 'stu.success',
                'target': 'new',
            }
        # else:
        #     self.yes_number = self.yes_number + 1
